home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / turbotut.arc / MANUAL.EXE / lha / CHAP9.TXT < prev    next >
Text File  |  1988-11-27  |  20KB  |  454 lines

  1.                            CHAPTER 9 - Records
  2.  
  3.  
  4.             We  come  to  the grandaddy of all  data  structures  in
  5.         Pascal,  the  RECORD.   A record is composed of a number  of
  6.         variables  any of which can be of any predefined data  type,
  7.         including  other records.   Rather than spend time trying to
  8.         define  a  record  in detail,  lets go right  to  the  first
  9.         example program, SMALLREC.  This is a program using nonsense
  10.         data that will illustrate the use of a record.
  11.  
  12.                            A VERY SIMPLE RECORD
  13.  
  14.             There is only one entry in the TYPE declaration part  of
  15.         the program,  namely the record identified by "description".
  16.         The record is composed of three fields, the "year", "model",
  17.         and  "engine" variables.   Notice that the three fields  are
  18.         each of a different type,  indicating that the record can be
  19.         of  mixed types.   You have a complete example of the way  a
  20.         record  is  defined  before  you.   It is  composed  of  the
  21.         identifier ("description"),  the reserved word  RECORD,  the
  22.         list of elements,  and followed by END;.  This is one of the
  23.         places   in   Pascal  where  an  END  is  used   without   a
  24.         corresponding BEGIN.   Notice that this only defines a TYPE,
  25.         it  does not define any variables.   That is done in the VAR
  26.         declaration where the variable "cars" is defined to have  10
  27.         complete  records of the type "description".   The  variable
  28.         "cars[1]" has three components, year, model, and engine, and
  29.         any  or  all of these components can be used to  store  data
  30.         pertaining to "cars[1]".
  31.  
  32.             In  order  to assign values to the various  fields,  the
  33.         variable name is followed by the sub-field with a separating
  34.         period.   Keep  in mind that "cars[1]" is a complete  record
  35.         containing three variables,  and to assign or use one of the
  36.         variables,  you  must  designate  which  sub-field  you  are
  37.         interested in.   See the program where the three fields  are
  38.         assigned  meaningless  data for  illustration.   The  "year"
  39.         field  is  assigned  an  integer  number  varying  with  the
  40.         subscript,   all   "model"  fields  are  assigned  the  name
  41.         "Duesenburg",  and  all "engine" variables are assigned  the
  42.         value "V8".   In order to further illustrate that there  are
  43.         actually  30  variables in use here,  a few are  changed  at
  44.         random  in  the next few statements,  being very careful  to
  45.         maintain   the  required  types  as  defined  in  the   TYPE
  46.         declaration part of the program.  Finally, all ten composite
  47.         variables,  consisting  of 30 actual variables in a  logical
  48.         grouping  are  printed  out using  the  same  "var.subfield"
  49.         notation described above.
  50.  
  51.             If the preceding description of a record is not clear in
  52.         your mind,  review it very carefully.  It's a very important
  53.         concept in Pascal,  and you won't have a hope of a chance of
  54.         understanding the next example until this one is clear.
  55.  
  56.  
  57.                                   Page 39
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                            CHAPTER 9 - Records
  68.  
  69.  
  70.  
  71.                               A SUPER RECORD
  72.  
  73.             Examine  the  Pascal  example file  BIGREC  for  a  very
  74.         interesting  record.   First  we  have a  constant  defined.
  75.         Ignore  it for the moment,  we will come back to  it  later.
  76.         Within  the TYPE declaration we have three records  defined,
  77.         and  upon close examination,  you will notice that the first
  78.         two  records are included as part of the definition  of  the
  79.         third record.   The record identified as "person",  actually
  80.         contains   9   variable  definitions,   three   within   the
  81.         "full_name" record,  three of its own,  and three within the
  82.         "date"  record.   This  is a TYPE declaration and  does  not
  83.         actually define any variables,  that is done in the VAR part
  84.         of the program.
  85.  
  86.             The  VAR  part  of the program  defines  some  variables
  87.         beginning  with the array of "friend" containing 50 (because
  88.         of  the  constant definition in the CONST part)  records  of
  89.         "person".   Since  "person" defines 9 fields,  we  have  now
  90.         defined  9  times 50 = 450 separate and distinct  variables,
  91.         each  with its own defined type.   Remember that  Pascal  is
  92.         picky about assigning data by the correct type.  Each of the
  93.         450  separate variables has its own type associated with it,
  94.         and the compiler will generate an error if you try to assign
  95.         any  of  those  variables the wrong  type  of  data.   Since
  96.         "person" is a TYPE definition, it can be used to define more
  97.         than  one variable,  and in fact it is used again to  define
  98.         three more records,  "self",  "mother", and "father".  These
  99.         three records are each composed of 9 variables,  so we  have
  100.         27  more  variables  which  we  can  manipulate  within  the
  101.         program.   Finally we have the variable "index" defined as a
  102.         simple byte type variable.
  103.  
  104.                     HOW TO MANIPULATE ALL OF THAT DATA
  105.  
  106.             In  the program we begin by assigning data to all of the
  107.         fields of "self".   Examining the first three statements  of
  108.         the main program,  we see the construction we learned in the
  109.         last  example program being used,  namely the period between
  110.         descriptor fields.   The main record is named "self", and we
  111.         are  interested  in the first part of it namely  the  "name"
  112.         part  of the person record.   Since the "name" part  of  the
  113.         person  record  is itself composed of three parts,  we  must
  114.         designate  which  part  of it we  are  interested  in.   The
  115.         complete description "self.name.first_name" is the  complete
  116.         description  of  the first name of "self" and is  the  first
  117.         assignment   statement   which  is  assigned  the  name   of
  118.         "Charley".   The next two fields are handled in the same way
  119.         and are self explanatory.
  120.  
  121.  
  122.  
  123.                                   Page 40
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                            CHAPTER 9 - Records
  134.  
  135.  
  136.                         WHAT IS THE WITH STATEMENT?
  137.  
  138.             Continuing on to the fourth field, the "city", there are
  139.         only  two  levels  required because "city"  is  not  another
  140.         record definition.  The fourth field is therefore completely
  141.         defined   by  "self.city".    Notice  the  "WITH  self   DO"
  142.         statement.   This  is a shorthand notation used with  record
  143.         definitions to simplify coding.   From the BEGIN at the next
  144.         statement  to the matching END;  about 10 statements  later,
  145.         any  variables within the "self" record are used  as  though
  146.         they had a "self." in front of them.   It greatly simplifies
  147.         coding  to be able to omit the leading identifier within the
  148.         WITH section of code.   You will see that  "city",  "state",
  149.         and  "zipcode"  are easily assigned values  without  further
  150.         reference to the "self" variable.   When we get to the "day"
  151.         part  of the birthday,  we are back to three levels and  the
  152.         complete  definition is "self.birthday.day" but once  again,
  153.         the  "self." part is taken care of automatically because  we
  154.         are still within the "WITH self DO" area.
  155.  
  156.             To  illustrate  the WITH statement further,  another  is
  157.         introduced namely "WITH birthday DO", and an area is defined
  158.         by  the  BEGIN  END pair.   Within this  area  both  leading
  159.         identifiers  are handled automatically to  simplify  coding,
  160.         and  "month" is equivalent to writing  "self.birthday.month"
  161.         if both WITH statements were removed.   You may be wondering
  162.         how   many   levels  of  nesting  are  allowed   in   record
  163.         definitions.   There  doesn't appear to be a limit according
  164.         to the Pascal definition, but we do get a hint at how far it
  165.         is possible to go.  In TURBO Pascal, you are allowed to have
  166.         WITH  statements  nested to nine levels,  and  it  would  be
  167.         worthless  to nest WITH statements deeper than the level  of
  168.         records.   Any  program  requiring more levels than nine  is
  169.         probably  far beyond the scope of your programming  ability,
  170.         and  mine,  for a long time.   Pascal implementations  other
  171.         than  TURBO  Pascal probably have their own  WITH  statement
  172.         nesting limitation. Check your reference manual.
  173.  
  174.             After assigning a value to the year,  the entire  record
  175.         of "self" is defined, all nine variables.
  176.  
  177.                         SUPER-ASSIGNMENT STATEMENTS
  178.  
  179.             The   next   statement,   "mother  :=  self;"  is   very
  180.         interesting.   Since both of these are records, both are the
  181.         same type of record, and both therefore contain 9 variables,
  182.         Pascal  is smart enough to recognize that,  and  assign  all
  183.         nine   values  contained  in  "self"  to  the  corresponding
  184.         variables of "mother".   So after one statement, "mother" is
  185.         completely  defined.   The next statement assigns  the  same
  186.         values  to the nine respective fields of "father",  and  the
  187.  
  188.  
  189.                                   Page 41
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                            CHAPTER 9 - Records
  200.  
  201.  
  202.         next  two  lines assign all 50 "friend" variables  the  same
  203.         data.   We  have therefore generated 450 + 27 = 477 separate
  204.         pieces  of data so far in this program.   We could print  it
  205.         all out,  but since it is nonsense data, it would only waste
  206.         time and paper.  The next three lines write out three sample
  207.         pieces of the data for your inspection.
  208.  
  209.                          WHAT GOOD IS ALL OF THIS
  210.  
  211.             It should be obvious to you that what this program does,
  212.         even  though  the  data  is  nonsense,  appears  to  be  the
  213.         beginning of a database management program,  which indeed it
  214.         is.  It is a crude beginning, and has a long way to go to be
  215.         useful, but you should see a seed for a useful program.
  216.  
  217.             Now to go back to the CONST as promised.   The number of
  218.         friends was defined as 50 and used for the size of the array
  219.         and in the assignment loop near the end of the program.  You
  220.         can  now edit this number and see how big this database  can
  221.         become on your computer.  If you are using TURBO Pascal, you
  222.         will  be limited to slightly more than 1000 because  of  the
  223.         64K  limitation of an executable program,  and the fact that
  224.         all  of this data is stored within that 64K  boundary.   See
  225.         how  big you can make the number of friends before  you  get
  226.         the  memory  overflow  message.   Keep the  number  in  mind
  227.         because  when we get to the chapter on Pointers and  Dynamic
  228.         Allocation,  you  will  see a marked increase  in  allowable
  229.         size, especially if you have a large amount of RAM installed
  230.         in your computer.
  231.  
  232.                              A VARIANT RECORD
  233.  
  234.             If  any part of this chapter is still unclear,  it would
  235.         be good for you to go back and review it at this time.   The
  236.         next  example  will  really  tax  your  mind  to  completely
  237.         understand  it,  especially  if the prior  material  is  not
  238.         clear.
  239.  
  240.             Examine  the  Pascal program VARREC for an example of  a
  241.         program with a variant record definition.   In this example,
  242.         we first define a scalar type,  namely "kind_of_vehicle" for
  243.         use  within  the record.   Then we have  a  record  defining
  244.         "vehicle",  intended  to  define several different types  of
  245.         vehicles,  each with different kinds of data.   It would  be
  246.         possible  to define all variables for all types of vehicles,
  247.         but  it  would  be a waste of storage space  to  define  the
  248.         number  of  tires for a boat,  or the  number  of  propeller
  249.         blades  used on a car or truck.   The variant record lets us
  250.         define  the data precisely for each vehicle without  wasting
  251.         data storage space.
  252.  
  253.  
  254.  
  255.                                   Page 42
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                            CHAPTER 9 - Records
  266.  
  267.  
  268.                            WHAT IS A TAG-FIELD?
  269.  
  270.             In the record definition we have the usual RECORD header
  271.         followed  by three variables defined in the same  manner  as
  272.         the records in the last two example programs.   Then we come
  273.         to the CASE statement.  Following this statement, the record
  274.         is  different  for  each of the four types  defined  in  the
  275.         associated  scalar definition.   The variable "what_kind" is
  276.         called  the tag-field and must be defined as a  scalar  type
  277.         prior  to  the  record definition.   The tag-field  is  what
  278.         selects the variant used,  when the program uses one of  the
  279.         variables with this record type.   The tag-field is followed
  280.         by  a colon and its type definition,  then the reserved word
  281.         OF.   A list of the variants is then given, with each of the
  282.         variants  having  the  variables  for  its  particular  case
  283.         defined.   The  list of variables for one variant is  called
  284.         the field list.
  285.  
  286.             A few rules are in order at this point.  The variants do
  287.         not have to have the same number of variables in each  field
  288.         list,  and in fact,  one or more of the variants may have no
  289.         variables  at all in its variant part.   If a variant has no
  290.         variables,  it  must still be defined with a pair  of  empty
  291.         parentheses followed by a semi-colon.   All variables in the
  292.         entire  variant  part  must have unique  names.   The  three
  293.         variables, "wheels", "tires", and "tyres", all mean the same
  294.         thing  to  the  user,  but they must be  different  for  the
  295.         compiler.   You may use the same identifiers again in  other
  296.         records  and  for  simple  variables anywhere  else  in  the
  297.         program.   The  Pascal compiler can tell which variable  you
  298.         mean by its context.  Using the same variable name should be
  299.         discouraged  as  bad  programming practice  because  it  may
  300.         confuse  you  or another person trying  to  understand  your
  301.         program at a later date.  The final rule is that the variant
  302.         part of the record must be the last part of it, and in fact,
  303.         the  last  part  of any or all variants can  itself  have  a
  304.         variant part to it.  That is getting pretty advanced for our
  305.         level of use of Pascal at this time however.
  306.  
  307.                          USING THE VARIANT RECORD
  308.  
  309.             We  properly define four variables with the record  type
  310.         and go on to examine the program itself.
  311.  
  312.             We  begin  by  defining  one of our  variables  of  type
  313.         "vehicle",  namely  the variable named  "ford".   The  seven
  314.         lines  assigning  values to "ford" are similar to the  prior
  315.         examples  with  the exception of the fourth  line.   In  the
  316.         fourth  line  the  tag-field which  selects  the  particular
  317.         variant used is set equal to the value "truck",  which is  a
  318.         scalar  definition,  not  a variable.   This means that  the
  319.  
  320.  
  321.                                   Page 43
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                            CHAPTER 9 - Records
  332.  
  333.  
  334.         variables  named  "motor",   "tires",   and  "payload"   are
  335.         available for use with the record "ford",  but the variables
  336.         named "wheels", "engine", "tyres", etc. are not available in
  337.         the record named "ford".
  338.  
  339.             Next,  lets  define the record "sunfish" as a boat,  and
  340.         define all of its variables.  All of sunfish's variables are
  341.         defined but in a rather random order to illustrate that they
  342.         need not be defined in a particular order.
  343.  
  344.             To  go even further in randomly assigning the  variables
  345.         to a record,  we redefine "ford" as having an "engine" which
  346.         it  can only have if it is a car.   This is one of the  fine
  347.         points  of  the Pascal record.   If you assign  any  of  the
  348.         variant  variables,  the record is changed to that  variant,
  349.         but  it  is  the programmers responsibility  to  assign  the
  350.         correct  tag-field  to  the  record,   not  Pascal's.   Good
  351.         programming practice would be to assign the tag-field before
  352.         assigning  any of the variant variables.   The remainder  of
  353.         the  "ford" variables are assigned to complete that  record,
  354.         the non-variant part remaining from the last assignment.
  355.  
  356.             The  variable  "mac"  is now set equal to  the  variable
  357.         "sunfish".   All  variables within the record are copied  to
  358.         "mac" including the tag-field, making "mac" a boat.
  359.  
  360.                   NOW TO SEE WHAT WE HAVE IN THE RECORDS
  361.  
  362.             We  have  assigned "ford" to be a  car,  and  two  boats
  363.         exist,  namely  "sunfish"  and "mac".   Since "schwinn"  was
  364.         never defined,  it has no data in it,  and is at this  point
  365.         useless.  The "ford" tag-field has been defined as a car, so
  366.         it should be true in the IF statement, and the first message
  367.         should  print.   The "sunfish" is not a bicycle,  so it will
  368.         not  print.   The  "mac" has been defined as a boat  in  the
  369.         single assignment statement, so it will print a message with
  370.         an  indication  that  all  of the data  in  the  record  was
  371.         transferred to its variables.
  372.  
  373.             Even  though  we  can make  assignment  statements  with
  374.         records,  they cannot be used in any mathematical operations
  375.         such as addition,  or multiplication.   They are simply used
  376.         for data storage.   It is true however,  that the individual
  377.         elements  in  a  record  can be  used  in  any  mathematical
  378.         statements legal for their respective types.
  379.  
  380.             One other point should be mentioned.   The tag-field can
  381.         be completely eliminated resulting in a "free union" variant
  382.         record.   This  is  possible  because  Pascal,  as  you  may
  383.         remember from above,  will automatically assign the  variant
  384.         required when you assign data to one of the variables within
  385.  
  386.  
  387.                                   Page 44
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                            CHAPTER 9 - Records
  398.  
  399.  
  400.         a variant.  This is the reason that all variables within any
  401.         of  the  variants must have unique names.   The  free  union
  402.         record  should be avoided in your early programming  efforts
  403.         because  you cannot test a record to see what variant it has
  404.         been assigned to.  It is definitely an advanced technique in
  405.         Pascal.
  406.  
  407.                            PROGRAMMING EXERCISE
  408.  
  409.         1.  Write  a simple program with a record to store the names
  410.             of five of your friends and display the names.
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.                                   Page 45
  454.